package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.SpeechRuleDO;
import com.xgimi.open.skill.datamanager.domain.mapper.SpeechRuleMapper;
import com.xgimi.open.skill.datamanager.domain.service.SpeechRuleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【speech_rule(说法规则表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:23:48
 */
@Service
public class SpeechRuleServiceImpl extends ServiceImpl<SpeechRuleMapper, SpeechRuleDO>
        implements SpeechRuleService {

    @Override
    public IPage<SpeechRuleDO> pageQuery(IPage<SpeechRuleDO> page, SpeechRuleDO mapperReq) {
        LambdaQueryWrapper<SpeechRuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getIntentId()), SpeechRuleDO::getIntentId, mapperReq.getIntentId());
        queryWrapper.like(!StringUtils.isEmpty(mapperReq.getRuleText()), SpeechRuleDO::getRuleText, mapperReq.getRuleText());
        queryWrapper.orderByDesc(SpeechRuleDO::getCreatedAt);
        return this.page(page, queryWrapper);
    }

    @Override
    public SpeechRuleDO queryById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<SpeechRuleDO> listByIntentId(Long intentId) {
        LambdaQueryWrapper<SpeechRuleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(intentId), SpeechRuleDO::getIntentId, intentId);
        return this.list(wrapper);
    }

    @Override
    public List<SpeechRuleDO> queryByIntentIds(List<Long> intentIds) {
        if (CollectionUtils.isEmpty(intentIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<SpeechRuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(!CollectionUtils.isEmpty(intentIds), SpeechRuleDO::getIntentId, intentIds);
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateByIdAndVersion(SpeechRuleDO speechRuleDO) {
        LambdaUpdateWrapper<SpeechRuleDO> updateWrapper = new LambdaUpdateWrapper<>();
        Integer updateVersion = speechRuleDO.getUpdateVersion();
        updateWrapper.eq(SpeechRuleDO::getUpdateVersion, updateVersion);
        updateWrapper.eq(SpeechRuleDO::getId, speechRuleDO.getId());
        // 版本自增
        speechRuleDO.setUpdateVersion(updateVersion + 1);
        return this.update(speechRuleDO, updateWrapper);
    }

    @Override
    public boolean removeByIntentIds(List<Long> intentIds) {
        if (CollectionUtils.isEmpty(intentIds)) {
            return true;
        }
        LambdaQueryWrapper<SpeechRuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SpeechRuleDO::getIntentId, intentIds);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean physicalDeleteByIntentIds(List<Long> intentIds) {
        if (CollectionUtils.isEmpty(intentIds)) {
            return Boolean.FALSE;
        }
        return this.baseMapper.physicalDeleteByIntentIds(intentIds);
    }

}




